package com.uobgroup.ews.uam.core.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.uobgroup.ews.uam.core.domain.ChangeLog;
import com.uobgroup.ews.uam.core.enums.OperateType;

@Repository
public class ChangeLogDaoImpl extends AbstractJpaDao<ChangeLog> implements ChangeLogDao {

   @Override
   public List<ChangeLog> findAll(int firstResult, int maxResult) {
      Query query = entityManager.createQuery("select c from ChangeLog c order by c.operateTime desc");
      return query.setFirstResult(firstResult).setMaxResults(maxResult).getResultList();
   }

   
   
   @Override
   public List<ChangeLog> findAll() {
      Query query = entityManager.createQuery("select c from ChangeLog c order by c.operateTime desc");
      return query.getResultList();
   }



   @Override
   public Object findEntityById(Long id, Class<?> objectClass) {
      return entityManager.find(objectClass, id);
   }

   @Override
   public Long count() {
      return entityManager.createQuery("SELECT COUNT(o) FROM ChangeLog o", Long.class).getSingleResult();
   }



   @Override
   public List<ChangeLog> find(OperateType operateType, String userId, Date fromDate, Date toDate, int firstResult,
         int maxResult) {
      String queryStr = "select c from ChangeLog c where lower(c.userId) like :userId";
      if(operateType != null){
         queryStr += " and c.operateType = :operateType";
      }
      if(fromDate != null){
         queryStr += " and c.operateTime >= :fromDate";
      }
      if(toDate != null){
         queryStr += " and c.operateTime <= :toDate";
      }
      queryStr += " order by c.operateTime desc";
      Query query = entityManager.createQuery(queryStr);
      query.setParameter("userId", "%" + userId.toLowerCase() + "%");
      if(operateType != null){
         query.setParameter("operateType", operateType);
      }
      if(fromDate != null){
         query.setParameter("fromDate", fromDate);
      }
      if(toDate != null){
         query.setParameter("toDate", toDate);
      }
      return query.setFirstResult(firstResult).setMaxResults(maxResult).getResultList();
   }



   @Override
   public Long count(OperateType operateType, String userId, Date fromDate, Date toDate) {
      String queryStr = "select count(c) from ChangeLog c where lower(c.userId) like :userId";
      if(operateType != null){
         queryStr += " and c.operateType = :operateType";
      }
      if(fromDate != null){
         queryStr += " and c.operateTime >= :fromDate";
      }
      if(toDate != null){
         queryStr += " and c.operateTime <= :toDate";
      }
      queryStr += " order by c.operateTime desc";
      Query query = entityManager.createQuery(queryStr, Long.class);
      query.setParameter("userId", "%" + userId.toLowerCase() + "%");
      if(operateType != null){
         query.setParameter("operateType", operateType);
      }
      if(fromDate != null){
         query.setParameter("fromDate", fromDate);
      }
      if(toDate != null){
         query.setParameter("toDate", toDate);
      }
      return (Long)query.getSingleResult();
   }
}
